package com.nd.qa.shareplate.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.nio.channels.FileChannel;

/**
 * 文件流操作类
 * @version 1.00 wuqiaomin 新建
 *
 */
public class FileUtil {
	public void createNew(String dirPath) {
		File dir = new File(dirPath);
		if (!dir.exists()) {
			dir.mkdir();
		}
	}

	public File createFile(String fileName, String Suffix) {
		File file = new File(fileName + Suffix);
		try {
			int repeatNum = 0;
			while (file.exists()) {
				repeatNum++;
				file = new File(fileName + repeatNum + Suffix);
			}
			file.createNewFile();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return file;
	}

	public void createDir(String dirPath) {
		String[] info = null;
		if (dirPath.contains("/")) {
			info = dirPath.split("/");
		} else {
			info = dirPath.split("\\");
		}
		String path = info[0];
		for (int i = 1; i < info.length; i++) {
			path += "/" + info[i];
			mkDir(path);
		}
	}

	private void mkDir(String dirPath) {
		File dir = new File(dirPath);
		if (!dir.getParentFile().exists()) {
			// dir.mkdir();	
			dir.getParentFile().mkdirs();
		}else if (!dir.exists()) {
			dir.mkdir();
		}
	}

	public boolean deleteDir(File dir) {
		if (dir.isDirectory()) {
			String[] children = dir.list();
			for (int i = 0; i < children.length; i++) {
				boolean success = deleteDir(new File(dir, children[i]));
				if (!success) {
					return false;
				}
			}
		}
		// 目录此时为空，可以删除
		boolean delete = dir.delete();
		// try {
		// Thread.sleep(500);
		// } catch (InterruptedException e) {
		// // TODO Auto-generated catch block
		// e.printStackTrace();
		// }
		return delete;
	}

	public void writeFile(File file, String content) {
		OutputStreamWriter writerStream;
		try {
			file.createNewFile();
			writerStream = new OutputStreamWriter(new FileOutputStream(file),
					"UTF-8");
			writerStream.write(content);
			writerStream.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void deleteFile(String filePath) {
		File file = new File(filePath);
		deleteFile(file);
	}

	public void deleteFile(File file) {
		if (file.exists()) {
			file.delete();
		}
	}

	public void copyFile(String source, String dest) {
		File sourceFile = new File(source);
		File destFile = new File(dest);
		FileInputStream fi = null;
		FileOutputStream fo = null;

		FileChannel in = null;
		FileChannel out = null;
		try {

			fi = new FileInputStream(sourceFile);
			fo = new FileOutputStream(destFile);
			in = fi.getChannel();// 得到对应的文件通道
			out = fo.getChannel();// 得到对应的文件通道
			in.transferTo(0, in.size(), out);// 连接两个通道，并且从in通道读取，然后写入out通道
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				fi.close();
				in.close();
				fo.close();
				out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	/**
	 * 生产文件 如果文件所在路径不存在则生成路径
	 * @param fileName 文件名 带路径
	 * @param isDirectory 是否为路径
	 * @return
	 */
	public static File buildFile(String fileName, boolean isDirectory) {
		File target = new File(fileName);
		if (isDirectory) {
			target.mkdirs();
		} else {
			if (!target.getParentFile().exists()) {
				target.getParentFile().mkdirs();
				target = new File(target.getAbsolutePath());
			}
		}
		System.out.println("创建路径:"+target.getAbsolutePath());
		return target;
	}
	/** 
	* 复制整个文件夹内容 
	* @param oldPath String 原文件路径 如：c:/fqf 
	* @param newPath String 复制后路径 如：f:/fqf/ff 
	* @return boolean 
	*/ 
	public void copyFolder(String oldPath, String newPath) { 

	try { 
	(new File(newPath)).mkdirs(); //如果文件夹不存在 则建立新文件夹 
	File a=new File(oldPath); 
	String[] file=a.list(); 
	File temp=null; 
	for (int i = 0; i < file.length; i++) { 
	if(oldPath.endsWith(File.separator)){ 
	temp=new File(oldPath+file[i]); 
	} 
	else{ 
	temp=new File(oldPath+File.separator+file[i]); 
	} 

	if(temp.isFile()){ 
	FileInputStream input = new FileInputStream(temp); 
	FileOutputStream output = new FileOutputStream(newPath + "/" + 
	(temp.getName()).toString()); 
	byte[] b = new byte[1024 * 5]; 
	int len; 
	while ( (len = input.read(b)) != -1) { 
	output.write(b, 0, len); 
	} 
	output.flush(); 
	output.close(); 
	input.close(); 
	} 
	if(temp.isDirectory()){//如果是子文件夹 
	copyFolder(oldPath+"/"+file[i],newPath+"/"+file[i]); 
	} 
	} 
	} 
	catch (Exception e) { 
	System.out.println("复制整个文件夹内容操作出错"); 
	e.printStackTrace(); 

	} 
	}
	public void IOCopy(String path,String path1){
	    File file = new File(path);
	    File file1 = new File(path1);
	    if(!file.exists()){
	     System.out.println(file.getName()+"文件不存在");
	    }else{
	     System.out.println("存在");
	    } 
	    byte[] b = new byte[(int) file.length()];
	    if(file.isFile()){
	    try {
	  FileInputStream is= new FileInputStream(file);
	  FileOutputStream ps= new FileOutputStream(file1);
	  is.read(b);
	  ps.write(b);
	 } catch (Exception e) {
	  e.printStackTrace();
	 }
	   }else if(file.isDirectory()){
	    if(!file.exists())
	     file.mkdir();
	    String[] list = file.list();
	    for(int i=0;i<list.length;i++){
	      this.IOCopy(path+"/"+list[i], path1+"/"+list[i]);
	    }
	   }
	}
}
